Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
postcss-js
Advanced tools
The postcss-js package allows you to use PostCSS, a tool for transforming CSS with JavaScript, in a JavaScript environment. This enables the processing of CSS styles with JavaScript objects, facilitating dynamic style generation and manipulation within JavaScript projects.
Parsing CSS to JS Objects
This feature allows the conversion of CSS styles into JavaScript objects, making it easier to manipulate CSS properties and values programmatically.
const postcssJs = require('postcss-js');
const autoprefixer = require('autoprefixer');
const root = postcssJs.parse({ color: 'red' });
console.log(root);
Applying PostCSS Plugins
Enables the use of PostCSS plugins, such as autoprefixer, directly on JavaScript objects representing CSS styles, allowing for advanced CSS processing and manipulation.
const postcssJs = require('postcss-js');
const autoprefixer = require('autoprefixer');
const prefixer = postcssJs.sync([ autoprefixer ]);
const prefixed = prefixer({ display: 'flex' });
console.log(prefixed);
Styled-components is a library for React and React Native that allows you to use component-level styles in your application. It uses tagged template literals to style your components. Unlike postcss-js, which focuses on transforming CSS with JavaScript, styled-components aim to enhance CSS for styling React component systems.
Emotion is a performant and flexible CSS-in-JS library that allows you to style applications quickly with string or object styles. It supports both the CSS and CSS-in-JS approaches, providing a more integrated styling solution for React applications compared to postcss-js, which is more focused on CSS transformation and manipulation.
Tailwind CSS is a utility-first CSS framework for rapidly building custom designs. Unlike postcss-js, which is a tool for transforming CSS with JavaScript, Tailwind provides a set of utility classes to build custom designs directly in your markup, promoting a different approach to styling web applications.
PostCSS for React Inline Styles, Radium, JSS and other CSS-in-JS.
For example, to use Stylelint, RTLCSS or postcss-write-svg plugins in your workflow.
const postcssJs = require('postcss-js')
const autoprefixer = require('autoprefixer')
const prefixer = postcssJs.sync([ autoprefixer ])
const style = prefixer({
userSelect: 'none'
})
style //=> {
// WebkitUserSelect: 'none',
// MozUserSelect: 'none',
// msUserSelect: 'none',
// userSelect: 'none'
// }
const postcss = require('postcss')
const postcssJs = require('postcss-js')
const style = {
top: 10,
'&:hover': {
top: 5
}
};
postcss().process(style, { parser: postcssJs }).then( (result) => {
result.css //=> top: 10px;
// &:hover { top: 5px; }
})
const postcss = require('postcss')
const postcssJs = require('postcss-js')
const css = '--text-color: #DD3A0A; @media screen { z-index: 1; color: var(--text-color) }'
const root = postcss.parse(css)
postcssJs.objectify(root) //=> {
// '--text-color': '#DD3A0A',
// '@media screen': {
// zIndex: '1',
// color: 'var(--text-color)'
// }
// }
sync(plugins): function
Create PostCSS processor with simple API, but with only sync PostCSS plugins support.
Processor is just a function, which takes one style object and return other.
async(plugins): function
Same as sync
, but also support async plugins.
Returned processor will return Promise.
parse(obj): Root
Parse CSS-in-JS style object to PostCSS Root
instance.
It converts numbers to pixels and parses [Free Style] like selectors and at-rules:
{
'@media screen': {
'&:hover': {
top: 10
}
}
}
This methods use Custom Syntax name convention, so you can use it like this:
postcss().process(obj, { parser: postcssJs })
objectify(root): object
Convert PostCSS Root
instance to CSS-in-JS style object.
Webpack may need some extra config for some PostCSS plugins.
Module parse failed
Autoprefixer and some other plugins need a json-loader to import data.
So, please install this loader and add to webpack config:
loaders: [
{
test: /\.json$/,
loader: "json-loader"
}
]
3.0.3
package.exports
Node.js 15 fix.FAQs
PostCSS for CSS-in-JS and styles in JS objects
The npm package postcss-js receives a total of 5,451,826 weekly downloads. As such, postcss-js popularity was classified as popular.
We found that postcss-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.